package com.andreiamariei.tema6.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.andreiamariei.tema6.entities.Actor;
import com.andreiamariei.tema6.entities.Category;
import com.andreiamariei.tema6.entities.Movie;
import com.andreiamariei.tema6.services.ActorService;
import com.andreiamariei.tema6.services.CategoryService;
import com.andreiamariei.tema6.services.MovieService;

/**
 * @author andrei.amariei, 24.11.2010 06:30:43
 * 
 */
@ManagedBean
@RequestScoped
public class MovieView {

	private String title;
	private String year;
	private String length;
	private List<String> categoryIds;
	private List<String> actorIds;
	private String message;
	private Movie movie;

	@EJB
	private CategoryService categoryService;
	
	@EJB
	private ActorService actorService;
	
	@EJB
	private MovieService movieService;
	
	public MovieView() {
		movie = new Movie();
	}
	
	public void setCategoryDao(CategoryService categoryDao) {
		this.categoryService = categoryDao;
	}
	
	public CategoryService getCategoryDao() {
		return categoryService;
	}
	
	public void setMovie(Movie entity) {
		this.movie = entity;
	}

	public Movie getMovie() {
		return movie;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public List<String> getCategoryIds() {
		return categoryIds;
	}

	public void setCategoryIds(List<String> categoryIds) {
		this.categoryIds = categoryIds;
	}

	public List<String> getActorIds() {
		return actorIds;
	}

	public void setActorIds(List<String> actorIds) {
		this.actorIds = actorIds;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public Object add() {
		
		try {
			List<String> categoryIds = this.getCategoryIds();
			List<Category> categs = new ArrayList<Category>(categoryIds.size());
			
			for (String categoryId : categoryIds) {
				Category category = categoryService.find(Long.valueOf(categoryId));
				categs.add(category);
			}
			this.movie.setCategories(categs);
			
			List<String> actorIds = this.getActorIds();
			List<Actor> actors = new ArrayList<Actor>(actorIds.size());
			
			for (String actorId : actorIds) {
				Actor actor = actorService.find(Long.valueOf(actorId));
				actors.add(actor);
			}
			this.movie.setActors(actors);
			
			movieService.create(this.movie);
			message = "success";
		} catch (Exception e) {
			e.printStackTrace();
			message = "failure";
		}
		return null;
	}
	
	public Object remove() {
		
		movieService.remove(this.movie);
		return null;
	}
	
	public List<Category> getAllCategories() {
		return categoryService.findAll();
	}
	
	public List<Actor> getAllActors() {
		return actorService.findAll();
	}
	
	public List<MovieView> getAllMovies() {
		List<Movie> movies = movieService.findAll();
		
		List<MovieView> movieViews = new ArrayList<MovieView>(movies.size());
		for (Movie movie : movies) {
			MovieView movieView = new MovieView();
			movieView.movie = movie;
			
			movieViews.add(movieView);
		}
		
		return movieViews;
	}
}
